﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Bus_proj.website
{
    public partial class cancel_ticket : System.Web.UI.Page
    {
        string date, time, date2,input_s;
        int sell_id, check, alertt;
        DataClasses1DataContext db = new DataClasses1DataContext();
        //int[] data_cancel= new int[6];
        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "/" + mount2 + "/" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }
                
            }       
        }

        protected void Button1_Click(object sender, EventArgs e)
        {           
            try
            {
                string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                input_s = TextBox1.Text;
                string ticket = @"select Table_Sale.Sale_id,Table_Around.Around_id,Table_Around.Around_date,Table_Route.Route_source,Table_Route.Route_destination,Table_Time.Time_time,Table_Sale.SSID,Table_SaleDetail.Seat_no
                            from Table_Sale
                            INNER join Table_Around
                                on Table_Sale.Around_id = Table_Around.Around_id
                            INNER join Table_Route
                                on Table_Around.Route_id = Table_Route.Route_id
                            INNER join Table_Time
                                on Table_Around.Time_id = Table_Time.Time_id
                            inner join Table_SaleDetail
	                            on Table_SaleDetail.Sale_id = Table_Sale.Sale_id
                            where Table_Sale.Sale_id = '" + input_s + "' and (SSID =1 or SSID =5)";
                string chk_ticket = @"select *
                                    from Table_changeticket
                                    where Sale_id ='" + input_s + "'";
                SqlCommand sqlcomm = new SqlCommand();
                SqlConnection sqlconn = new SqlConnection(Connection);
                SqlDataAdapter da = new SqlDataAdapter(ticket, sqlconn);
                SqlDataAdapter da2 = new SqlDataAdapter(chk_ticket, sqlconn);
                DataTable dt = new DataTable();
                DataTable dt2 = new DataTable();
                da.Fill(dt);
                da2.Fill(dt2);
                if (dt.Rows.Count <= 0)
                {            
                    //Panel1.Visible = true;
                    //Label2.Visible = true;
                    //Button2.Visible = false;
                    /////////////////////
                    string ticket2 = @"select *
                                    from Table_payment
                                    inner join  Table_Sale
                                    on Table_payment.Sale_id = Table_Sale.Sale_id
                                    INNER join Table_Around
                                        on Table_Sale.Around_id = Table_Around.Around_id
                                    INNER join Table_Route
                                        on Table_Around.Route_id = Table_Route.Route_id
                                    INNER join Table_Time
                                        on Table_Around.Time_id = Table_Time.Time_id
                                    inner join Table_SaleDetail
	                                    on Table_SaleDetail.Sale_id = Table_payment.Sale_id
                                     where Table_payment.payment_id = '" + input_s + "' and (SSID =1 or SSID =5) and Table_payment.payment_status = 1 ";
                    SqlCommand sqlcomm2 = new SqlCommand();
                    SqlConnection sqlconn2 = new SqlConnection(Connection);
                    SqlDataAdapter da_ticket2 = new SqlDataAdapter(ticket2, sqlconn);
                    DataTable dt_ticket2 = new DataTable();
                    da_ticket2.Fill(dt_ticket2);
                    if (dt_ticket2.Rows.Count <= 0)
                    {
                        Panel1.Visible = false;
                        Panel2.Visible = true;
                        Label3.Visible = true;
                        Button3.Visible = false;
                    }
                    else
                    {
                        date = ConvestToDate_check(dt_ticket2.Rows[0]["Around_date"].ToString());
                        time = dt_ticket2.Rows[0]["Time_time"].ToString();
                        date2 = date + " " + time + ":00";
                        string testt = ((DateTime.Parse(date2)).AddHours(-4)).ToString();
                        DateTime test = DateTime.Parse(testt);
                        if (test >= DateTime.Now)
                        {
                            GridView2.DataSource = dt_ticket2;
                            GridView2.DataBind();
                            Panel2.Visible = true;
                            Panel1.Visible = false;
                            //Session["seat_no"] = input_s;
                            Button3.Visible = true;
                        }
                        else
                        {
                            Panel1.Visible = false;
                            Panel2.Visible = false;
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วก่อนเดินทาง 4 ชั่วโมงได้');", true);
                            //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                        }
                    }                    
                }
                else
                {
                    if (Int32.Parse(dt.Rows[0]["SSID"].ToString()) == 1 || Int32.Parse(dt.Rows[0]["SSID"].ToString()) == 5)
                    {
                        if (Int32.Parse(dt.Rows[0]["SSID"].ToString()) == 1)
                        {
                            if (dt2.Rows.Count <= 0)
                            {
                                Session["count"] = 20;
                            }
                            else
                            {
                                Session["count"] = 50;
                            }
                        }
                        else
                        {
                            Session["count"] = 50;
                        }                        
                        date = ConvestToDate_check(dt.Rows[0]["Around_date"].ToString());
                        time = dt.Rows[0]["Time_time"].ToString();
                        date2 = date + " " + time + ":00";
                        //string date2 = "2557/02/14 23:00:00";
                        string testt = ((DateTime.Parse(date2)).AddHours(-4)).ToString();
                        DateTime test = DateTime.Parse(testt);
                        //    using (SqlConnection con = new SqlConnection(Connection))
                        //    {
                        //        using (SqlCommand cmd = new SqlCommand("TESTLOGIN", con))
                        //        {
                        //            cmd.CommandType = CommandType.StoredProcedure;
                        //            cmd.Parameters.Add(new SqlParameter("@start", date2.ToString().Trim()));
                        //            SqlParameter returnParameter = cmd.Parameters.Add("@start", SqlDbType.VarChar);
                        //            returnParameter.Direction = ParameterDirection.ReturnValue;
                        //            con.Open();
                        //            cmd.ExecuteNonQuery();
                        //            check = (int)returnParameter.Value;
                        //        }
                        //    }
                        //    if (check < 240)
                        //    {
                        //        Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้ก่อนรถออก 4 ชั่วโมง');</script>");
                        //        alertt = 1;
                        //    }
                        //    Label2.Visible = false;
                        //    Button2.Visible = true;
                        //}
                        if (test >= DateTime.Now)
                        {
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                            Panel1.Visible = true;
                            Panel2.Visible = false;
                            Session["seat_no"] = input_s;
                            Button2.Visible = true;
                        }
                        else
                        {
                            Panel1.Visible = false;
                            Panel2.Visible = false;
                            ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วก่อนเดินทาง 4 ชั่วโมงได้');", true);
                            //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                        }
                    }
                    else
                    {
                        Panel1.Visible = false;
                        Panel2.Visible = false;
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);
                        //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
                    }
                    
                }
            }
            catch (Exception)
            {
                Panel1.Visible = false;
                Panel2.Visible = false;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถคืนตั๋วได้');", true);
                //Response.Write(@"<script language='javascript'>alert('ไม่สามารถคืนตั๋วได้');</script>");
            }           
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            Button2.Visible = false;
            Label2.Visible = false;         
            Response.Redirect("cancel_ticket_ans.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            int i = 0;
            int data_count=0;
            int data_count_print = 0;
            //string[] data_ticket = new string[6];
            //string[] data_ticket_print = new string[6];
            string data_ticket="";
            string data_ticket_print="";
            string data_cancel = "";
            foreach (GridViewRow gr in GridView2.Rows)
            {
                CheckBox CheckBox1 = (CheckBox)gr.FindControl("CheckBox1");
                if (CheckBox1.Checked == true)
                {
                    if (data_count==0)
                    {
                        data_ticket = GridView2.Rows[i].Cells[1].Text;
                        data_count++;
                    }
                    else
                    {
                        data_ticket = data_ticket + "," + GridView2.Rows[i].Cells[1].Text;
                    }                   
                }
                else
                {
                    if (data_count_print == 0)
                    {
                        data_ticket_print = GridView2.Rows[i].Cells[1].Text;
                        data_count_print++;
                    }
                    else
                    {
                        data_ticket_print = data_ticket_print + "," + GridView2.Rows[i].Cells[1].Text;
                    }                       
                }
                i++;
            }
            data_count = 0;
            string Connection3 = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            for (int k = 0; k < i; k++)
            {
                try
                {
                    string[] aaa = data_ticket.Split(',');
                    string ticket21 = @"select *
                                    from Table_Sale
                                    where Table_Sale.Sale_id = '" + aaa[k] + "'";
                    string chk_ticket2 = @"select *
                                    from Table_changeticket
                                    where Sale_id ='" + aaa[k] + "'";
                    SqlCommand sqlcomm3 = new SqlCommand();
                    SqlConnection sqlconn3 = new SqlConnection(Connection3);
                    SqlDataAdapter da_ticket21 = new SqlDataAdapter(ticket21, sqlconn3);
                    SqlDataAdapter da_chk_ticket2 = new SqlDataAdapter(chk_ticket2, sqlconn3);
                    DataTable dt_ticket21 = new DataTable();
                    DataTable dt_chk_ticket2 = new DataTable();
                    da_ticket21.Fill(dt_ticket21);
                    da_chk_ticket2.Fill(dt_chk_ticket2);
                    if (dt_ticket21.Rows.Count <= 0)
                    {

                    }
                    else
                    {
                        if (Int32.Parse(dt_ticket21.Rows[0]["SSID"].ToString()) == 1 || Int32.Parse(dt_ticket21.Rows[0]["SSID"].ToString()) == 5)
                        {
                            if (Int32.Parse(dt_ticket21.Rows[0]["SSID"].ToString()) == 1)
                            {
                                if (dt_chk_ticket2.Rows.Count <= 0)
                                {
                                    if (data_count == 0)
                                    {
                                        data_cancel = 20.ToString();
                                        data_count++;
                                    }
                                    else
                                    {
                                        data_cancel = data_cancel + "," + 20;
                                    }
                                }
                                else
                                {
                                    if (data_count == 0)
                                    {
                                        data_cancel = 50.ToString();
                                        data_count++;
                                    }
                                    else
                                    {
                                        data_cancel = data_cancel + "," + 50;
                                    }
                                }
                            }
                            else
                            {
                                if (data_count == 0)
                                {
                                    data_cancel = 50.ToString();
                                    data_count++;
                                }
                                else
                                {
                                    data_cancel = data_cancel + "," + 50;
                                }
                            }

                        }
                    }
                }
                catch (Exception)
                {                    
                    
                }                    
            }
            string[] data1 = data_cancel.Split(',');
            string[] data2 = data_ticket.Split(',');
            string[] data3 = data_ticket_print.Split(',');
            Session["data_cancel"] = data1;
            Session["data_ticket"] = data2;
            Session["data_ticket_print"] = data3;
            Response.Redirect("cancel_ticket_ans2.aspx");
        }
    }
}